import dayjs from 'dayjs';
import store from './../store';
import storage from 'good-storage';

// 深拷贝
function typeOf(obj) {
    const toString = Object.prototype.toString
    const map = {
        '[object Boolean]': 'boolean',
        '[object Number]': 'number',
        '[object String]': 'string',
        '[object Function]': 'function',
        '[object Array]': 'array',
        '[object Date]': 'date',
        '[object RegExp]': 'regExp',
        '[object Undefined]': 'undefined',
        '[object Null]': 'null',
        '[object Object]': 'object'
    }
    return map[toString.call(obj)]
}

export function deepCopy(data) {
    const t = typeOf(data)
    let o

    if (t === 'array') {
        o = []
    } else if (t === 'object') {
        o = {}
    } else {
        return data
    }

    if (t === 'array') {
        for (let i = 0; i < data.length; i++) {
            o.push(deepCopy(data[i]))
        }
    } else if (t === 'object') {
        for (let i in data) {
            o[i] = deepCopy(data[i])
        }
    }
    return o
}

// 监听返回键
export function closeApp() {
    let exitFlag = false;
    window.api.addEventListener({
        name: 'keyback'
    }, function () {
        if (!exitFlag) {
            window.api.toast({
                msg: '再按一次退出应用',
                duration: 2000,
                location: 'bottom'
            });
            exitFlag = true;
        } else {
            window.api.closeWidget({
                silent: true
            });
        }
        setTimeout(function () {
            exitFlag = false;
        }, 2000);
    });
}

// 提示
export function alertMsg(msg) {
    return dayjs().format("HH:mm:ss") + " " + msg
}

//接单成功 提示
export function audio() {
    let audioStreamer = window.api.require("audioStreamer");
    audioStreamer.openPlayer(
        {
            path: "http://pub.pk1165.com/wawabang.mp3"
        },
        function () {

        }
    );
}
// 保存已登录账号信息
export function saveInfo(data) {
    let iconItems =
    store.getters.iconItems.length !== 0
          ? store.getters.iconItems
          : storage.get("iconItems");
    iconItems[data.index].user = data.user;
    iconItems[data.index].pass = data.pass;
    iconItems[data.index].type = data.type;
    store.commit("SET_ICONITEMS", iconItems);
    storage.set("iconItems", iconItems);
}
